Test Failed
Push — master ( 96bf47...0b3edf )
by Dmytro
03:06 queued 12s
created

index.js ➔ createMockApp   A

Complexity

Conditions 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 30
rs 9.0833
c 0
b 0
f 0
cc 5
1
import path from 'path';
2
import jsonServer from 'json-server';
3
import fse from 'fs-extra';
4
import * as fixtures from './fixtures';
5
6
const filesDir = path.join(__dirname, './files');
7
8
const { users, actions } = fixtures;
9
const badCode = 404;
10
11
function createMockApp() {
12
    const server = jsonServer.create();
13
    const router = jsonServer.router({ users });
14
    const middlewares = jsonServer.defaults();
15
16
    server.use(middlewares);
17
18
    server.start = async function (port) {
19
        let app;
20
21
        server.use('/api', router);
22
        server.post('/format/:format', async (req, res) => {
23
            if (req.params.format === 'xml') {
24
                res.type('application/xml');
25
26
                return res.send('<status>OK</status>');
27
            }
28
29
            if (req.params.format === 'Buffer') {
30
                const content = await fse.readFile(path.join(filesDir, '1.txt'));
31
32
                res.writeHead(200, {
33
                    'Content-Type'        : 'mimetype',
34
                    'Content-disposition' : 'attachment;filename=1.txt',
35
                    'Content-Length'      : content.length
36
                });
37
38
                return res.end(Buffer.from(content, 'binary'));
39
            }
40
41
            res.sendStatus(badCode);
42
        });
43
44
        await new Promise(res => {
45
            app = server.listen(port, res);
46
        });
47
48
        return app;
49
    };
50
51
    return server;
52
}
53
54
export default createMockApp;
55
export { fixtures, actions };
56
57